/**
 * 
 */
let Utils = require('Utils')
let {Global, GameState} = require('Global')
let {Card, Texas} = require('Poker')

var Game = cc.Class({
    extends: cc.Component,
    properties: {
        sock: cc.Node,
        ui: cc.Node,
        table: cc.Node
    },

    statics: {
        instance: null
    },


    // use this for initialization
    onLoad() {
        Global.gameInstance = this
        this.table = this.table.getComponent('TxTable')
        this.sock = this.sock.getComponent('TxSocket')
        window.sock = this.io.node
        this.io.handle = this
        this.ui = this.ui.getComponent('UIManager')

        let {sid, st, gt, isd, name, roomId, avatar, goldLeastCount } = Utils.parseQueryString(window.location.search)
        if (!sid || !st || !roomId) {
            this.ui.showAlert({
                msg: '参数不对',
                text0: '好的',
                fn0: () => {
                }
            })
            return
        }
        Global.state.isDealer = isd
        Global.user.name = decodeURI(name)
        Global.user.avatar = avatar
        Global.state.roomId = roomId
        Global.state.sid = sid
        Global.state.st = st
        Global.state.goldLeastCount = goldLeastCount
        this.sock.connect({ socialId: sid, socialType: st }, this)

    },

    gameDidLoadFinished() {
        if (typeof (NATIVE) !== "undefined") {
            window.updateGamePoints = (info) => {
                Global.user.gold = info.count
            }
            NATIVE.gameDidLoadFinished()
        }
    },

    /**
     * 取消创建房间
     */
    cancelConfiged() {
        if (cc.sys.isMobile)
            window.location.href = 'busgame://needClose'
    },

    updateGamePoints(info) {
        console.log(`通知本地金币变动 ${info.count} ${info.type}`)
        Global.user.gold += info.count
        if (typeof (NATIVE) !== "undefined") {
            NATIVE.updateGamePoints(info.count, info.type)
        }
    },

    /**
     * 判断玩家钱是否足够
     */
    checkMoney(info) {
    },

    sit() {
    },

    /* 工具 */
    isDealer() {
        return Global.user._id === Global.room.dealerId
    },
})